package com.csdj.t309.controller;


import com.csdj.t309.common.MessageCode;
import com.csdj.t309.common.TAddressFormatter;
import com.csdj.t309.common.WebResult;
import org.apache.log4j.Logger;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class ExceptionController {

    private Logger log = Logger.getLogger("exception");

    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)//可以处理异常的类型
    public WebResult exception(RuntimeException e){//异常
//        e.printStackTrace();
        log.error("error",e);//日志中打印
        return new WebResult(MessageCode.FAIL);
    }

    @InitBinder
    public void bind(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new TAddressFormatter());
    }



}
